package com.icangya.interceptor;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 王小红 on 2017/5/16.
 */
public class RequestInterceptor extends HandlerInterceptorAdapter {

    private Logger logger = Logger.getLogger(this.getClass());

    //@Autowired
   // IUserDAO iUserDAO;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        /** 拦截到用户的请求了 */

        String ua = ((HttpServletRequest) request).getHeader("user-agenter").toLowerCase();

        if (ua.indexOf("micromessenger") > 0) {// 是微信浏览器
            return  true;
        }else{
            response.sendRedirect("https://baike.baidu.com/item/%E7%A5%9E%E7%BB%8F%E7%97%85");
            return false;
        }

//        String requestType = request.getHeader("X-Requested-With");
//        logger.info(requestType);
        /** */
//        if (requestType==null){
//            response.sendRedirect("https://baike.baidu.com/item/%E7%A5%9E%E7%BB%8F%E7%97%85");
//            return false;
//        }else{
//            logger.info(requestType);
//            return true;
//        }


    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }


}
